package org.sgx.gwteditors.testgallery.gallery;

import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.widget.client.TextButton;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.cell.client.EditTextCell;
import com.google.gwt.cell.client.SelectionCell;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.sgx.gwteditors.client.impl1.edbean.EditableBean;
import org.sgx.gwteditors.client.util.TestUtil;

import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.core.client.GWT;

public class Test1 extends FlexTable{
	private FlexTable itemsTable;
	private FlexTable itemList;
	public Test1() {
		
		itemsTable = new FlexTable();
		setWidget(0, 0, itemsTable);
		
		HorizontalPanel itemlistButtonPanel = new HorizontalPanel();
		itemlistButtonPanel.setSpacing(4);
		itemsTable.setWidget(0, 0, itemlistButtonPanel);
		itemlistButtonPanel.setHeight("100%");
		
		Label lblAdd = new Label("add");
		itemlistButtonPanel.add(lblAdd);
		
		Label lblDelete = new Label("delete");
		itemlistButtonPanel.add(lblDelete);
		
		Label lblUp = new Label("up");
		itemlistButtonPanel.add(lblUp);
		
		Label lblDown = new Label("down");
		itemlistButtonPanel.add(lblDown);
		
		itemList = new FlexTable();
		itemsTable.setWidget(1, 0, itemList);
		itemList.setSize("100%", "100%");
		
		TextButton txtbtnNewButton = new TextButton("New button");
		itemList.setWidget(0, 0, txtbtnNewButton);
		
		CellTable<Contact> cellTable = new CellTable<Contact>();
		itemsTable.setWidget(1, 1, cellTable);
		
		Column<Contact, Boolean> column_2 = new Column<Contact, Boolean>(new CheckboxCell()) {
			@Override
			public Boolean getValue(Contact object) {
				return (Boolean) null;
			}
		};
		cellTable.addColumn(column_2, "New Column");
		
		Column<Contact, String> column = new Column<Contact, String>(new EditTextCell()) {
			@Override
			public String getValue(Contact object) {
				return (String) null;
			}
		};
		cellTable.addColumn(column, "New Column");
		
		Column<Contact, String> column_1 = new Column<Contact, String>(new SelectionCell(new ArrayList<String>())) {
			@Override
			public String getValue(Contact object) {
				return (String) null;
			}
		};
		cellTable.addColumn(column_1, "New Column");
		getCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_TOP);
		

		ListDataProvider<Contact> dataProvider = new ListDataProvider<Contact>();

		// Connect the table to the data provider
		dataProvider.addDataDisplay(cellTable);
		List<Contact> list = dataProvider.getList();
		for(Contact c : getAllContacts()) {
			list.add(c); 
		}
	}
	
	
	
	//model
		public static abstract class Contact implements EditableBean {
			String name, address;
			Boolean male; 
			public String getName() {
				return name;
			}
			public void setName(String name) {
				this.name = name;
			}
			public String getAddress() {
				return address;
			}
			public void setAddress(String address) {
				this.address = address;
			}
			public Boolean getMale() {
				return male;
			}
			public void setMale(Boolean male) {
				this.male = male;
			} 		
		}
		private static List<Contact> getAllContacts() {
			final TestUtil testUtil = TestUtil.getInstance(); 
			List<Contact> l = new LinkedList<Contact>(); 
			for (int i = 0; i < 12; i++) {
				Contact c = GWT.create(Contact.class);
				c.setName(testUtil.randomLetters(testUtil.randomBetween(10,20))); 
				c.setAddress(testUtil.randomLetters(testUtil.randomBetween(10,20))); 
				//order properties
				c.getProperties().reorder(new String[]{"name", "address", "male"}); 
				l.add(c); 
			}
			return l; 
		}

}
